﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Threading;
using NovoDataBase;
using System.Data;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using NovoSerDesEva;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Controls.Primitives;



namespace NovoSerDes.Forms
{
    public partial class Erro_Status : UIPage
    {
        private SerDesComm commDev;
        private NovoDataSet DataSet;
        private List<RegisterMap> regMapList;
        private List<NovoDataSet> novoDataSets;
        private RegisterMap regMap;
        private Dictionary<string, string> tabPageSearchText = new Dictionary<string, string>();
        private List<string> iniFilePaths = new List<string>();
        private List<string> sections = new List<string>();
        private List<List<string>> keys = new List<List<string>>();

        System.Timers.Timer timer; 
        public Erro_Status()
        {
            novoDataSets = MainForm.GetDataSetList();
            timer = new System.Timers.Timer();
            InitializeComponent();
        }

        private void ERRB_Form_Load(object sender, EventArgs e)
        {
            string path = System.IO.Path.Combine(Application.StartupPath, "INI FILE");
            if (!Directory.Exists(path))
            {
                UIMessageDialog.ShowErrorDialog(FindForm(), "找不到ini文件");
                foreach (Control control in this.Controls)
                {
                    control.Enabled = false;
                }
            }
            else
            {
                uiTabControl1.Controls.Clear();
                string[] iniFiles = Directory.GetFiles(path, "*.ini");
                iniFilePaths.Clear();
                iniFilePaths.AddRange(iniFiles);   //所有的ini文件路径集合

                foreach (string iniFilePath in iniFilePaths)
                {
                    if (!File.Exists(iniFilePath))
                    {
                        // 文件不存在，进行相应的处理（例如记录日志或给出提示）
                        UIMessageDialog.ShowErrorDialog(FindForm(), "找不到ini文件");
                        foreach (Control control in this.Controls)
                        {
                            control.Enabled = false;
                        }
                        return;
                    }

                    IniFiles.IniFile iniFile = new IniFiles.IniFile(iniFilePath);
                    TabPage tabPage = new TabPage(iniFilePath);   //为每个ini文件创建一个tab
                    tabPage.Name = iniFile.ReadString("Name", "name", null);
                    tabPage.Text = tabPage.Name;    //tab name = ini name
                    uiTabControl1.TabPages.Add(tabPage);

                    tabPageSearchText[tabPage.Name] = txt_Search.Text;

                    foreach (NovoDataSet dataSet in novoDataSets)
                    {
                        if (dataSet.DevInfo.FilePath.ToUpper().Contains(tabPage.Name.ToUpper()))
                        {
                            regMap = dataSet.RegMap;
                            break;
                        }                      
                    }

                    sections.Clear();
                    sections.AddRange(iniFile.ReadSections);   //ini文件所有的section集合
                                                               //每个tab下放一个FlowLayoutPanel，自动布局
                    UIFlowLayoutPanel uiFlowLayoutPanel = new UIFlowLayoutPanel();
                    uiFlowLayoutPanel.Dock = DockStyle.Fill;
                    uiFlowLayoutPanel.Padding = new Padding(30);
                    tabPage.Controls.Add(uiFlowLayoutPanel);
                    keys.Clear();
                    foreach (string section in sections)
                    {
                        if (section.ToUpper() == "NAME") continue;
                        UIGroupBox uIGroupBox = new UIGroupBox();   //每个section是一个groupBox，name = section name
                        uIGroupBox.Name = section;
                        uIGroupBox.Text = section;
                        uIGroupBox.Padding = new Padding(10);
                        uIGroupBox.Margin = new Padding(30);
                        uIGroupBox.AutoScroll = true;

                        List<string> list = new List<string>(iniFile.ReadKeyNames(section));
                        keys.Add(list);
                        //keys.AddRange(iniFile.ReadKeyNames(section));   //获取键值
                        string[] keySection = iniFile.ReadKeyNames(section);
                        FlowLayoutPanel mainFlowLayoutPanel = new FlowLayoutPanel();    //每个groupBox里面放一个FlowLayoutPanel，自动布局
                        mainFlowLayoutPanel.Dock = DockStyle.Fill;
                        mainFlowLayoutPanel.FlowDirection = FlowDirection.TopDown; // 垂直排列
                        mainFlowLayoutPanel.AutoSize = true;
                        mainFlowLayoutPanel.WrapContents = false; // 不自动换行
                        mainFlowLayoutPanel.AutoScroll = true;
                        uIGroupBox.Controls.Add(mainFlowLayoutPanel);
                        uIGroupBox.Font = new System.Drawing.Font("思源黑体 CN Bold", 8F);

                        foreach (var key in keySection)
                        {
                            //UIPanel uiPanelIner = new UIPanel();
                            //uiPanelIner.AutoSize = true;

                            UILabel label = new UILabel();  //label显示键值
                                                            //每个TabLayoutPanel放一个键值对
                            UITableLayoutPanel uiPanelIner = new UITableLayoutPanel();
                            uiPanelIner.RowCount = 1;
                            uiPanelIner.ColumnCount = 2;
                            uiPanelIner.Name = key;
                            uiPanelIner.Size = new System.Drawing.Size(mainFlowLayoutPanel.Width, 40);
                            uiPanelIner.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, uiPanelIner.Width * 8 / 10));
                            mainFlowLayoutPanel.Controls.Add(uiPanelIner);

                            label.Text = key;
                            label.Name = key;
                            label.Dock = DockStyle.Fill;
                            label.Font = new System.Drawing.Font("思源黑体 CN Bold", 8F);
                            string value = iniFile.ReadString(section, key, null);
                            label.AutoSize = true;
                            uiPanelIner.Controls.Add(label);
                            UIAvatar uIAvatar = new UIAvatar();
                            uIAvatar.Name = "Avatar";
                            uIAvatar.Shape = UIShape.Circle;
                            switch (value.ToUpper())
                            {
                                case "ERROR":

                                    uIAvatar.Symbol = this.uiAvatarGreen.Symbol;
                                    uIAvatar.FillColor = this.uiAvatarGreen.FillColor;
                                    uIAvatar.AvatarSize = this.uiAvatarGreen.AvatarSize;
                                    break;

                                case "CORRECT":

                                    uIAvatar.Symbol = this.uiAvatarRed.Symbol;
                                    uIAvatar.FillColor = this.uiAvatarRed.FillColor;
                                    uIAvatar.AvatarSize = this.uiAvatarRed.AvatarSize;
                                    break;

                                default:
                                    uIAvatar.Symbol = this.uiAvatarSliver.Symbol;
                                    uIAvatar.FillColor = this.uiAvatarSliver.FillColor;
                                    uIAvatar.AvatarSize = this.uiAvatarSliver.AvatarSize;
                                    break;
                            }
                            uiPanelIner.Controls.Add((uIAvatar));
                            uiPanelIner.AutoSize = true;
                        }
                        uIGroupBox.Size = new System.Drawing.Size(mainFlowLayoutPanel.Width * 10 / 8, mainFlowLayoutPanel.Height * 2);
                        uiFlowLayoutPanel.Controls.Add(uIGroupBox);
                        uIGroupBox.AutoSize = true;
                    }
                }
               

            }
            uiTabControl1_SelectedIndexChanged(sender, e);
        }

        //private void uiTabControl1_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    TabPage selectedTabpage = uiTabControl1.SelectedTab;
        //    selectedTabpage.Controls.Clear();
        //    IniFiles.IniFile iniFile = null;

            
        //    foreach(RegisterMap map in regMapList)
        //    {
        //        if (map.GetName.ToUpper().Contains(selectedTabpage.Name.ToUpper())){
        //            regMap = map;
        //        }
        //    }
        //    //string projPath = System.IO.Path.Combine(Application.StartupPath, "nsproj file");
        //    //importFile.RestoreDirectory = true;
        //    //if (Directory.Exists(projPath))
        //    //{
        //    //    string[] nsprojFiles = Directory.GetFiles(projPath, "*.nsproj");
        //    //    foreach (string filePath in nsprojFiles)
        //    //    {
        //    //        if (filePath.Contains(selectedTabpage.Name))
        //    //        {
        //    //            IFormatter formatter = new BinaryFormatter();
        //    //            Stream destream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
        //    //            DataSet = formatter.Deserialize(destream) as NovoDataSet;
        //    //            destream.Close();

        //    //            regMap = DataSet.RegMap;

        //    //            ////测试语句
        //    //            //// Init tabs with created data tables
        //    //            //Console.WriteLine(regMap["u_ssc_pll4p0g_reg2"].RegName);
        //    //            //Console.WriteLine(regMap["u_ssc_pll4p0g_reg2"].RegAddress);
        //    //            //uint add = regMap["u_ssc_pll4p0g_reg2"].RegAddress;
        //    //            //uint data;
        //    //            //Register reg = regMap["u_ssc_pll4p0g_reg2"];
        //    //            //BitField bitField = reg["pll_4p0g_intpr[2:0]"];
        //    //            //Console.WriteLine("pll_4p0g_freq_offset_valid 的值是{0}", bitField.BFValue);
        //    //            //commDev.IICRead(add, out data);
        //    //            //Console.WriteLine(commDev.IsOpen);
        //    //            //Console.WriteLine("u_ssc_pll4p0g_reg2 的地址是 {0}，值是{1}", add, data);
        //    //        }
        //    //    }

        //    //}
        //    //else
        //    //{
        //    //    UIMessageDialog.ShowErrorDialog(FindForm(), "找不到nsproj文件");
        //    //}

        //    foreach (string iniFilePath in iniFilePaths)
        //    {
        //        if (!File.Exists(iniFilePath))
        //        {
        //            // 文件不存在，进行相应的处理（例如记录日志或给出提示）
        //            continue;
        //        }
        //        IniFiles.IniFile file = new IniFiles.IniFile(iniFilePath);
        //        if (selectedTabpage.Name == file.ReadString("Name", "name", null))
        //        {
        //            iniFile = file;
        //            break;
        //        }
        //    }
        //    if (iniFile == null)
        //    {
        //        // 未找到对应的 INI 文件，进行相应的处理（例如记录日志或给出提示）
        //    }
        //    else
        //    {
                
        //        sections.Clear();
        //        sections.AddRange(iniFile.ReadSections);   //ini文件所有的section集合
        //                                                   //每个tab下放一个FlowLayoutPanel，自动布局
        //        UIFlowLayoutPanel uiFlowLayoutPanel = new UIFlowLayoutPanel();
        //        uiFlowLayoutPanel.Dock = DockStyle.Fill;
        //        uiFlowLayoutPanel.Padding = new Padding(30);
        //        selectedTabpage.Controls.Add(uiFlowLayoutPanel);
        //        keys.Clear();
        //        foreach (string section in sections)
        //        {
        //            if (section.ToUpper() == "NAME") continue;
        //            UIGroupBox uIGroupBox = new UIGroupBox();   //每个section是一个groupBox，name = section name
        //            uIGroupBox.Name = section;
        //            uIGroupBox.Text = section;
        //            uIGroupBox.Padding = new Padding(10);
        //            uIGroupBox.Margin = new Padding(30);
        //            uIGroupBox.AutoScroll = true;

        //            List<string> list = new List<string>(iniFile.ReadKeyNames(section));
        //            keys.Add(list);
        //            //keys.AddRange(iniFile.ReadKeyNames(section));   //获取键值
        //            string[] keySection = iniFile.ReadKeyNames(section);
        //            FlowLayoutPanel mainFlowLayoutPanel = new FlowLayoutPanel();    //每个groupBox里面放一个FlowLayoutPanel，自动布局
        //            mainFlowLayoutPanel.Dock = DockStyle.Fill;
        //            mainFlowLayoutPanel.FlowDirection = FlowDirection.TopDown; // 垂直排列
        //            mainFlowLayoutPanel.AutoSize = true;
        //            mainFlowLayoutPanel.WrapContents = false; // 不自动换行
        //            mainFlowLayoutPanel.AutoScroll = true;
        //            uIGroupBox.Controls.Add(mainFlowLayoutPanel);
        //            uIGroupBox.Font = new System.Drawing.Font("思源黑体 CN Bold", 8F);

        //            foreach (var key in keySection)
        //            {
        //                //UIPanel uiPanelIner = new UIPanel();
        //                //uiPanelIner.AutoSize = true;

        //                UILabel label = new UILabel();  //label显示键值
        //                                                //每个TabLayoutPanel放一个键值对
        //                UITableLayoutPanel uiPanelIner = new UITableLayoutPanel();
        //                uiPanelIner.RowCount = 1;
        //                uiPanelIner.ColumnCount = 2;
        //                uiPanelIner.Name = key;
        //                uiPanelIner.Size = new System.Drawing.Size(mainFlowLayoutPanel.Width, 40);
        //                uiPanelIner.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, uiPanelIner.Width * 8 / 10));
        //                mainFlowLayoutPanel.Controls.Add(uiPanelIner);

        //                label.Text = key;
        //                label.Name = key;
        //                label.Dock = DockStyle.Fill;
        //                label.Font = new System.Drawing.Font("思源黑体 CN Bold", 8F);
        //                string value = iniFile.ReadString(section, key, null);
        //                label.AutoSize = true;
        //                uiPanelIner.Controls.Add(label);
        //                UIAvatar uIAvatar = new UIAvatar();
        //                uIAvatar.Name = "Avatar";
        //                uIAvatar.Shape = UIShape.Circle;
        //                switch (value.ToUpper())
        //                {
        //                    case "ERROR":

        //                        uIAvatar.Symbol = this.uiAvatarGreen.Symbol;
        //                        uIAvatar.FillColor = this.uiAvatarGreen.FillColor;
        //                        uIAvatar.AvatarSize = this.uiAvatarGreen.AvatarSize;
        //                        break;

        //                    case "CORRECT":

        //                        uIAvatar.Symbol = this.uiAvatarRed.Symbol;
        //                        uIAvatar.FillColor = this.uiAvatarRed.FillColor;
        //                        uIAvatar.AvatarSize = this.uiAvatarRed.AvatarSize;
        //                        break;

        //                    default:
        //                        uIAvatar.Symbol = this.uiAvatarSliver.Symbol;
        //                        uIAvatar.FillColor = this.uiAvatarSliver.FillColor;
        //                        uIAvatar.AvatarSize = this.uiAvatarSliver.AvatarSize;
        //                        break;
        //                }
        //                uiPanelIner.Controls.Add((uIAvatar));
        //                uiPanelIner.AutoSize = true;
        //            }
        //            uIGroupBox.Size = new System.Drawing.Size(mainFlowLayoutPanel.Width * 10 / 8, mainFlowLayoutPanel.Height * 2);
        //            uiFlowLayoutPanel.Controls.Add(uIGroupBox);
        //            uIGroupBox.AutoSize = true;
        //            //uIGroupBox.AutoSizeMode = AutoSizeMode.GrowAndShrink;
        //        }
        //    }

        //}
        private void uiTabControl1_SelectedIndexChanged(object sender, EventArgs e){
            txt_Search.Text = tabPageSearchText[uiTabControl1.SelectedTab.Name];
            TabPage tabPage = this.uiTabControl1.SelectedTab;
            foreach (NovoDataSet dataSet in novoDataSets)
            {
                if (dataSet.DevInfo.FilePath.ToUpper().Contains(tabPage.Name.ToUpper()))
                {
                    regMap = dataSet.RegMap;
                    break;
                }               
            }
        }
        private void uiButton18_Click(object sender, EventArgs e)
        {
            string text = txt_Search.Text;
            tabPageSearchText[uiTabControl1.SelectedTab.Name] = text;
            TabPage selectedTabpage = uiTabControl1.SelectedTab;
            selectedTabpage.Controls.Clear();
            IniFiles.IniFile iniFile = null;
            foreach (string iniFilePath in iniFilePaths)
            {
                if (!File.Exists(iniFilePath))
                {
                    // 文件不存在，进行相应的处理（例如记录日志或给出提示）
                    continue;
                }
                IniFiles.IniFile file = new IniFiles.IniFile(iniFilePath);
                if (selectedTabpage.Name == file.ReadString("Name", "name", null))
                {
                    iniFile = file;
                    break;
                }
            }
            if (iniFile == null)
            {
                // 未找到对应的 INI 文件，进行相应的处理（例如记录日志或给出提示）
            }
            else
            {
                List<string> sectionsSelct = new List<string>();
                List<List<string>> keySectionSelct = new List<List<string>>();
                foreach (string sec in iniFile.ReadSections)
                {
                    if (sec.IndexOf(text, StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        sectionsSelct.Add(sec);
                    }
                }

                if (sectionsSelct.Count != 0)
                {
                    foreach (string section in sectionsSelct)
                    {
                        List<string> key = new List<string>(iniFile.ReadKeyNames(section));
                        keySectionSelct.Add(key);
                    }
                }
                else
                {
                    foreach (string section in iniFile.ReadSections)
                    {
                        if (section.ToUpper() == "NAME") continue;

                        List<string> keySection = new List<string>();                    
                        foreach (string key in iniFile.ReadKeyNames(section))
                        {
                            if (key.IndexOf(text, StringComparison.OrdinalIgnoreCase) >= 0)
                            {
                                keySection.Add(key);
                            }
                        }
                        if (keySection.Count != 0)
                        {
                            sectionsSelct.Add(section);
                            keySectionSelct.Add(keySection);
                        }
                    }
                }
                UIFlowLayoutPanel uIFlowLayoutPanel = CreateFlowLayoutPanel(sectionsSelct, keySectionSelct, iniFile);
                uIFlowLayoutPanel.Dock = DockStyle.Fill;
                selectedTabpage.Controls.Add(uIFlowLayoutPanel);
            }
        }


        private UIFlowLayoutPanel CreateFlowLayoutPanel(List<string> sections, List<List<string>> keySection, IniFiles.IniFile iniFile)
        {
            UIFlowLayoutPanel uiFlowLayoutPanel = new UIFlowLayoutPanel();
            uiFlowLayoutPanel.Dock = DockStyle.Fill;
            uiFlowLayoutPanel.Padding = new Padding(30);

            for (int i = 0; i < sections.Count; i++)
            {
                if (sections[i].ToUpper() == "NAME") continue;
                UIGroupBox uIGroupBox = new UIGroupBox();
                uIGroupBox.Name = sections[i];
                uIGroupBox.Text = sections[i];
                uIGroupBox.Padding = new Padding(10);
                uIGroupBox.Margin = new Padding(30);
                uIGroupBox.AutoScroll = true;


                FlowLayoutPanel mainFlowLayoutPanel = new FlowLayoutPanel();
                mainFlowLayoutPanel.Dock = DockStyle.Fill;
                mainFlowLayoutPanel.FlowDirection = FlowDirection.TopDown;
                mainFlowLayoutPanel.AutoSize = true;
                //mainFlowLayoutPanel.WrapContents = false; 
                mainFlowLayoutPanel.AutoScroll = false;
                uIGroupBox.Controls.Add(mainFlowLayoutPanel);
                uIGroupBox.Font = new System.Drawing.Font("思源黑体 CN Bold", 8F);

                for (int j = 0; j < keySection[i].Count; j++)
                {
                    //UIPanel uiPanelIner = new UIPanel();
                    //uiPanelIner.AutoSize = true;

                    UILabel label = new UILabel();

                    UITableLayoutPanel uiPanelIner = new UITableLayoutPanel();
                    uiPanelIner.RowCount = 1;
                    uiPanelIner.ColumnCount = 2;
                    uiPanelIner.Name = keySection[i][j];
                    uiPanelIner.Size = new System.Drawing.Size(mainFlowLayoutPanel.Width, 40);
                    uiPanelIner.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, uiPanelIner.Width * 8 / 10));
                    mainFlowLayoutPanel.Controls.Add(uiPanelIner);

                    label.Text = keySection[i][j];
                    label.Name = keySection[i][j];
                    label.Dock = DockStyle.Fill;
                    label.Font = new System.Drawing.Font("思源黑体 CN Bold", 8F);
                    string value = iniFile.ReadString(sections[i], keySection[i][j], null);
                    label.AutoSize = true;
                    uiPanelIner.Controls.Add(label);
                    UIAvatar uIAvatar = new UIAvatar();
                    uIAvatar.Name = "Avatar";
                    uIAvatar.Shape = UIShape.Circle;
                    switch (value.ToUpper())
                    {
                        case "ERROR":

                            uIAvatar.Symbol = this.uiAvatarGreen.Symbol;
                            uIAvatar.FillColor = this.uiAvatarGreen.FillColor;
                            uIAvatar.AvatarSize = this.uiAvatarGreen.AvatarSize;
                            break;

                        case "CORRECT":

                            uIAvatar.Symbol = this.uiAvatarRed.Symbol;
                            uIAvatar.FillColor = this.uiAvatarRed.FillColor;
                            uIAvatar.AvatarSize = this.uiAvatarRed.AvatarSize;
                            break;

                        default:
                            uIAvatar.Symbol = this.uiAvatarSliver.Symbol;
                            uIAvatar.FillColor = this.uiAvatarSliver.FillColor;
                            uIAvatar.AvatarSize = this.uiAvatarSliver.AvatarSize;
                            break;
                    }
                    uiPanelIner.Controls.Add((uIAvatar));
                    uiPanelIner.AutoSize = true;
                }
                //uIGroupBox.Size = new System.Drawing.Size(mainFlowLayoutPanel.Width * 10 / 8, mainFlowLayoutPanel.Height * 2);
                uiFlowLayoutPanel.Controls.Add(uIGroupBox);
                uIGroupBox.AutoSize = true;
                uIGroupBox.AutoScroll = false;
                uIGroupBox.PerformLayout();
                uiFlowLayoutPanel.PerformLayout();
                uIGroupBox.AutoSizeMode = AutoSizeMode.GrowAndShrink;
                //uIGroupBox.AutoScroll = false;
            }
            uiFlowLayoutPanel.ScrollBarColor = System.Drawing.Color.Red;

            return uiFlowLayoutPanel;
        }

        private void btn_Refesh_Click(object sender, EventArgs e)
        {       
            RefreshData();
        }

        private async void Timer_Elapsed(object sender, EventArgs e)
        {
            //this.BeginInvoke(new MethodInvoker(RefreshData));
            if (!this.IsDisposed)
            {
                await Task.Run(() =>
                {
                    if(!this.IsDisposed)
                    {
                        this.BeginInvoke(new MethodInvoker(RefreshData));
                    }
                });
            }
            
        }

        private void RefreshData()
        {
            setComm();
            if (commDev == null || !commDev.IsOpen)
            {
                ShowErrorNotifier("CH347未打开");
                return;
            }

            TabPage selectTable = uiTabControl1.SelectedTab;
            foreach (Control controlUiFlowPanel in selectTable.Controls)
            {
                foreach (Control controlFlowPanel in controlUiFlowPanel.Controls)
                {
                    foreach (Control controlUiGroupBox in controlFlowPanel.Controls)
                    {
                        string registerName = controlUiGroupBox.Name;
                        Register reg = regMap[registerName];
                        if (reg != null)    //加入放错机制，防止名字错误
                        {
                            UInt32 tempData;
                            lock (new object())//线程安全，保证读取时值不会被改变
                            {
                                commDev.IICRead(reg.RegAddress, out tempData);
                            }
                            reg.RegValue = tempData;
                        }
                        else
                        {
                            UIMessageDialog.ShowErrorDialog(FindForm(), registerName + "不存在");
                            continue;
                        }

                        foreach (Control controlFlowPanelIner in controlUiGroupBox.Controls)
                        {
                            foreach (Control controlTablePanel in controlFlowPanelIner.Controls)
                            {
                                BitField bitField = reg[controlTablePanel.Name];
                                UIAvatar avatar = (UIAvatar)controlTablePanel.GetControl("Avatar");
                                if (bitField != null)
                                {
                                    switch (bitField.BFValue)
                                    {
                                        case 1:
                                            avatar.Shape = UIShape.Circle;
                                            avatar.Icon = UIAvatar.UIIcon.Symbol;
                                            avatar.FillColor = uiAvatarRed.FillColor;
                                            break;
                                        case 0:
                                            avatar.Shape = UIShape.Circle;
                                            avatar.Icon = UIAvatar.UIIcon.Symbol;
                                            avatar.FillColor = uiAvatarGreen.FillColor;
                                            break;
                                        default:
                                            avatar.FillColor = uiAvatarSliver.FillColor;
                                            avatar.Icon = UIAvatar.UIIcon.Text;
                                            avatar.Shape = UIShape.Square;
                                            avatar.Dock = DockStyle.Fill;
                                            avatar.Text = bitField.BFValue.ToString("X");
                                            avatar.ForeColor = Color.Black;
                                            break;
                                    }
                                }
                            }
                        }
                    }
                }
            }


        }

        private void setComm()
        {
            Dictionary<string, SerDesComm> pairs = MainForm.IniSerCommPairs;
            if (pairs.ContainsKey(uiTabControl1.SelectedTab.Name))
            {
                commDev = pairs[uiTabControl1.SelectedTab.Name];
            }
            else { commDev = null; }
        }
        private void UpdateTimerInterval()
        {
            double time;
            if (double.TryParse(combox_time.Text, out  time) && time != 0)
            {
                timer.Interval = (int)(1000 * time);
                timer.Elapsed += Timer_Elapsed;
                timer.Start();
            }
            else
            {
                UIMessageDialog.ShowErrorDialog(FindForm(), "时间格式无效!!!");
            }

            
        }

        private void uiSwitch1_ValueChanged(object sender, bool value)
        {
            if (switch_auto.Active)
            {
                UpdateTimerInterval();
            }
            else
            {
                timer.Stop();
            }
        }

        private void txt_Search_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                // 执行搜索功能，这里执行搜索的方法是 Search()
                uiButton18_Click(sender, e);
            }
        }
    }
}